package com.cnzhy.esclientrhl.controller;

import com.cnzhy.esclientrhl.index.ProductInfoIndex;
import com.cnzhy.esclientrhl.repository.ProductInfoRepository;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhaoyan5
 */
@RestController
public class ProductInfoController {

    private ProductInfoRepository prodRepository;

    /**
     * 使用setter 方法注入
     */
    @Autowired
    public void setProductInfoRepository(ProductInfoRepository prodRepository) {
        this.prodRepository = prodRepository;
    }

    //测试URL: http://127.0.0.1:8888/prod/add
    @GetMapping("/prod/add")
    public String add() throws Exception {
        ProductInfoIndex productInfoIndex = new ProductInfoIndex();
        productInfoIndex.setProductId("1");
        productInfoIndex.setProductName("涪陵榨菜");
        productInfoIndex.setAppliName("新增产品-涪陵榨菜");
        productInfoIndex.setRiskCode("aa1");
        productInfoIndex.setSumPremium(1);
        prodRepository.save(productInfoIndex);
        return "新增产品成功：" + new Timestamp(new Date().getTime());
    }

    //测试URL: http://127.0.0.1:8888/prod/addList
    @GetMapping("/prod/addList")
    public String addList() throws Exception {
        ProductInfoIndex indexDemo2 = new ProductInfoIndex();
        indexDemo2.setProductId("2");
        indexDemo2.setAppliName("新增列表产品2");
        indexDemo2.setRiskCode("新增列表产品2");
        indexDemo2.setSumPremium(2);
        ProductInfoIndex indexDemo3 = new ProductInfoIndex();
        indexDemo3.setProductId("3");
        indexDemo3.setAppliName("新增列表产品3");
        indexDemo3.setRiskCode("新增列表产品3");
        indexDemo3.setSumPremium(3);
        prodRepository.save(Arrays.asList(indexDemo2, indexDemo3));
        return "新增列表测试成功：" + new Timestamp(new Date().getTime());
    }

    //测试URL: http://127.0.0.1:8888/prod/update
    @GetMapping("/prod/update")
    public String update() throws Exception {
        ProductInfoIndex productInfoIndex = new ProductInfoIndex();
        productInfoIndex.setProductId("1");
        productInfoIndex.setProductName("变更产品名称-小康股份");
        productInfoIndex.setAppliName("a999999");
        productInfoIndex.setRiskCode("aa9999999");
        productInfoIndex.setSumPremium(99999);
        prodRepository.update(productInfoIndex);
        return "修改成功：" + new Timestamp(new Date().getTime());
    }

    //测试URL: http://127.0.0.1:8888/prod/delete
    @GetMapping("/prod/delete")
    public String delete() throws Exception {
        ProductInfoIndex productInfoIndex = new ProductInfoIndex();
        productInfoIndex.setProductId("1");
//        productInfoIndex.setAppliName("a999999");
//        productInfoIndex.setRiskCode("a9999999");
//        productInfoIndex.setSumPremium(99999);
        prodRepository.delete(productInfoIndex);
        return "删除成功：" + new Timestamp(new Date().getTime());
    }

    //测试URL: http://127.0.0.1:8888/prod/query
    @GetMapping("/prod/query")
    public List<ProductInfoIndex> query() throws Exception {
        QueryBuilder queryBuilder = QueryBuilders.matchPhrasePrefixQuery("productName", "涪陵");
        List<ProductInfoIndex> search = prodRepository.search(queryBuilder);
        return search;
    }

    @GetMapping("/prod/queryALL")
    public List<ProductInfoIndex> queryALL() throws Exception {
        List<ProductInfoIndex> search = prodRepository.search(QueryBuilders.matchAllQuery());
        return search;
    }

    //测试URL: http://127.0.0.1:8888/prod/index
    @GetMapping("/prod/index")
    public List<ProductInfoIndex> searchIndex() throws Exception {
        SearchRequest searchRequest = new SearchRequest("1");
        List<ProductInfoIndex> search = prodRepository.search(QueryBuilders.matchQuery("1", "1"));
        return search;
    }
}
